<?php
namespace Pay\Controller;


use Think\Controller;
use plugins\functions\MemcacheLock;

class PayController extends Controller
{
    private  $cachKey="user_pay_key";
    public  function __construct()
    {
    }



    //添加任务余额支付
    /**
     *
     *
     **/
    public  function  balancePay($userId,$money,$goodsId,$remake,$paytype=1,$type="renwu"){
        $goodsInfo =[];
        $userInfo = [];
        $user_money =0;
        if(!empty($goodsId)){
            $goodsInfo = D("goods")->where(array("goods_id"=>$goodsId))->find();
        }
        if(!empty($userId)){
            $userInfo = D("users")->where(array("user_id"=>$userId))->find();
            if(!empty($userInfo)){
                $user_money = $userInfo["user_money"];
            }
        }
        if($user_money-$money<0){
            return ["code"=>0,"msg"=>"余额不足"];
        }
        //开始扣除个人余额 plugins\functions\MemcacheLock
        
        //防止高并发
        include_once  "plugins/functions/MemcacheLock.php";
        $cachelock =$this->cachKey."_balancePay_".$userId;
        $MemcacheLock = new MemcacheLock();

        $lock = $MemcacheLock->addLock($cachelock, 120);
        if (!$lock) {
            return  ["code"=>0,"msg"=>"出现错误"];
        }
       $res= setUserMoneyRepot($userId,"-".$money,$type,$remake,$paytype,$goodsId);
       
       accountLog($userId, ($money * -1), 0,$remake);
       if(empty($res)){
           $MemcacheLock->releaseLock($cachelock);
           return  ["code"=>0,"msg"=>"出现错误"];
       }
       
       if($type=="renwu"){
           D("goods")->where(array("goods_id"=>$goodsId))->save(array("is_on_sale"=>1,"is_pay"=>1,"pay_time"=>date ("Y-m-d H:i:s",time ()) ));
           
       }
        $MemcacheLock->releaseLock($cachelock);
        return  ["code"=>1,"msg"=>"支付成功"];
    }
    
    
    public function pay(){
        
    }




}